package com.csg.ioms.controller;

import cn.hutool.core.util.RandomUtil;
import com.csg.ioms.model.dto.UserDTO;
import com.csg.ioms.result.Result;
import com.csg.ioms.utils.JwtUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

/**
 * <p>
 *  测试登录
 * </p>
 *
 * @author GeorgeChan 2020/2/3 17:37
 * @version 1.0
 * @since jdk1.8
 */
@RestController
@RequestMapping("/api/system")
public class LoginController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private JwtUtils jwtUtils;
    /**
     * 跳过认证的地址
     */
    @Value("jwt.config.skip-auth-urls")
    private String[] skipAuthUrls;
    /**
     * 有效时长
     */
    @Value("jwt.config.effective-time")
    private String effectiveTime;
    /**
     * token加密的key
     */
    @Value("jwt.config.secret-key")
    private String secretKey;
    /**
     * 认证签发人
     */
    @Value("jwt.config.issuer")
    private String issuer;


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody UserDTO userDTO) {
        ArrayList<Object> roles = Lists.newArrayList();
        roles.add("admin");
        Map<String, Object> map = Maps.newHashMap();
        map.put("role", roles);

        String jwt = jwtUtils.createJWT(RandomUtil.randomString(6), jwtUtils.getIssuer(), userDTO.getUsername(), Long.valueOf(jwtUtils.getEffectiveTime()) * 60 * 1000, map);
        return new Result(true, HttpStatus.OK.value(), "登陆成功", jwt);
    }
}
